package com.project.restaurant.user.controller;

import com.project.restaurant.common.api.ResponseResult;
import com.project.restaurant.common.api.ServiceReturn;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.common.key.SessionAttribute;
import com.project.restaurant.pojo.User;
import com.project.restaurant.redis.service.RedisClient;
import com.project.restaurant.user.dto.EmailAuthCodeParam;
import com.project.restaurant.user.dto.LoginParam;
import com.project.restaurant.user.service.LoginService;
import com.project.restaurant.user.vo.UserPublicInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(value = "登录接口",tags = "登录接口")
@RestController
@RequestMapping("/user/login")
@Slf4j
public class LoginController {

    @Autowired
    LoginService loginService;
    @Autowired
    RedisClient redisClient;

    @ApiOperation(value = "账户密码登录",notes = "")
    @ApiResponse(code = 200, message = "操作成功", response = UserPublicInfoVo.class)
    @PostMapping(value = "")
    public ResponseResult<UserPublicInfoVo> login(@ApiIgnore HttpSession session,
                                                  @ApiParam(value = "登录信息", required = true) @RequestBody @Validated LoginParam loginParam){
        //进行登录判断，如果数据库中存在用户名为account，并且密码也是为password
        ServiceReturn<User> result = loginService.login(loginParam);
        User nowLoginUser = result.getReturnData();
        //判断是否登录校验成功，
        if(nowLoginUser!=null){
            log.info("进行登录");
            //如果成功，就保存到session会话中
            session.setAttribute(SessionAttribute.NOW_LOGIN_USER,nowLoginUser);
        }
        return ResponseResult.auto(result.getStateCode(), UserPublicInfoVo.getUserSafeInfoVo(nowLoginUser));
    }

    @ApiOperation(value = "账户邮件登录",notes = "")
    @ApiResponse(code = 200, message = "操作成功", response = UserPublicInfoVo.class)
    @PostMapping(value = "/email")
    public ResponseResult<UserPublicInfoVo> login(@ApiIgnore HttpSession session,
                                                  @ApiParam(value = "登录信息", required = true) @RequestBody @Validated EmailAuthCodeParam emailLoginParam){
        ServiceReturn<User> result = loginService.emailLogin(emailLoginParam);
        User nowLoginUser = result.getReturnData();
        //判断是否登录校验成功，
        if(nowLoginUser!=null){
            //如果成功，就保存到session会话中
            session.setAttribute(SessionAttribute.NOW_LOGIN_USER,nowLoginUser);
        }
        return ResponseResult.auto(result.getStateCode(), UserPublicInfoVo.getUserSafeInfoVo(nowLoginUser));
    }


//    @GetMapping("/obtain/authCode")
//    public ResponseResult<String> getLoginAuthCode(HttpSession session){
//        ServiceReturn<String> result = loginService.generatorAuthCode(session.getId());
//        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
//    }

    @ApiOperation(value = "发送登录邮件",notes = "")
    @ApiResponse(code = 200, message = "操作成功", response = Boolean.class)
    @GetMapping(value = "/obtain/email/authCode/{email}")
    public ResponseResult<Boolean> sendLoginAuthCodeToEmail(@ApiParam(value = "目标邮件", required = true) @PathVariable("email") String email){
        ServiceReturn<Boolean> result = loginService.sendAuthCodeToEmail(email);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

}
